// 引入express
const express = require("express");
// 引入数据库
const sql = require("../pool.js");
// 创建路由器对象
const route = express.Router();
// 动物增加路由
route.post("/animalAdd", (req, res) => {
  sql.query("insert into zm_animal set ?", [req.body], (err, r) => {
    if (err) {
      throw err;
    }
    console.log(r);

    if (r.affectedRows) {
      res.send("增加成功!");
    } else {
      res.send("增加失败!");
    }
  });
});
//页面查询id直接
route.post("/selectid", (req, res) => {
  sql.query("select * from zm_animal where aid=?", [req.body.aid], (err, r) => {
    if (err) {
      throw err;
    }
    console.log(r);

    res.send(r);
  });
});
//查询
route.post("/animalselect", (req, res) => {
  sql.query("select * from zm_animal ", [req.body], (err, r) => {
    if (err) {
      throw err;
    }
    // console.log(r);

    res.send(r);
  });
});
//修改
route.post("/animalupdate", (req, res) => {
  sql.query(
    "UPDATE zm_animal set longitude=?,latitude=?,rate=? where aname=? ",
    [req.body.longitude, req.body.latitude, req.body.rate, req.body.aname],
    (err, r) => {
      if (err) {
        throw err;
      }
      console.log(r);

      if (r.affectedRows) {
        res.send("修改成功!");
      } else {
        res.send("修改失败!");
      }
    }
  );
});
module.exports = route;
